package com.lz.ovuola.repository.mysql.impl;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.lz.ovuola.entity.UserDo;
import com.lz.ovuola.repository.mysql.MyDynamicRepository;

/**
 * 官方api：http://docs.spring.io/spring-data/jpa/docs/1.11.1.RELEASE/reference/
 * html/
 * 
 * @author fz
 *
 */
@Repository("userRepository")
@CacheConfig(cacheNames = "user")
public interface UserRepository extends MyDynamicRepository<UserDo, Integer> {

	UserDo findByUserName(String userName);

	UserDo findByAppId(String appId);

	@Cacheable(key = "#p0+'_table_user_cache'")
	UserDo findOne(Integer id);

	@SuppressWarnings("unchecked")
	@CachePut(key = "#p0.id+'_table_user_cache'")
	UserDo save(UserDo user);

	@CacheEvict(key = "#p0.id+'_table_user_cache'")
	void delete(UserDo usesr);

	// nativeQuery
	@Query(value = "select * from user u where u.id = ?1", nativeQuery = true)
	UserDo findNativeById(String id);

	// Using named parameters
	// @Query("select u from UserDo u where u.userName = :userName")
	UserDo findByPassword(@Param("userName") String password);

	// 通过hardwareEquipment查找
	UserDo findByHardwareEquipment(String hardwareEquipment);

	//
	// @CacheEvict(key = "#p0.id+'_table_user_cache'")
	// void remove(UserDo user);
	//
	// @Async
	// Future<UserDo> findById(Integer id);

	// List<UserDo> findByUserNameAndPassword(String userName, String password);

	// 排序
	// List<UserDo> findByUserNameOrderByPasswordAsc(String userName);

	/**
	 * 多属性排序 // Order order1 = Order.asc("userName"); // Order order2 =
	 * Order.desc("password"); // List<Order> list=new ArrayList<Order>(); //
	 * list.add(order1); // list.add(order2); // Sort sort1 = new Sort(list);
	 * 
	 * @param userName
	 * @param sort
	 * @return
	 */
	// List<UserDo> findByUserName(String userName, Sort sort);
	//
	// // 分页
	// Page<UserDo> findByUserName(String userName, Pageable pageable);//
	// PageRequest
	//
	// // 去重
	// List<UserDo> findDistinctUserDoByUserNameOrPassword(String userName,
	// String password);
	//
	// // 取前几条或后几条
	// UserDo findFirstOrderByUserNameAsc();

	/**
	 * Page<UserDo> queryTop10ByUserName(String userName, Pageable pageable);
	 * new Specification<UserDo> () {
	 * 
	 * public Predicate toPredicate(Root<UserDo> root, CriteriaQuery<?> query,
	 * CriteriaBuilder cb) { Path<String> userName = root.get("userName");
	 * Path<String> password = root.get("password");
	 * 
	 * query.where(cb.like(userName, "%李%"), cb.like(password, "%王%"));
	 * //这里可以设置任意条查询条件
	 * 
	 * return null; } };
	 * 
	 * @param specification
	 * @return
	 */
	// List<UserDo> findSpecification(Specification<UserDo> specification);

	/**
	 * where x.age is null
	 * 
	 * @param userName
	 * @return
	 */
	// List<UserDo> findByUserNameIsNull(String userName);

}
